import { routerRedux } from 'dva/router';
import { message } from 'antd';
import { isResponerCode } from '../utils/utils';
import {
    fetchlImportList,
    fetchBusinessDetails,
    queryBusinessStatus,
    submitBrack,
    againSubmitBrank,
    submitUploadVideo,
    submitDetails
} from '../services/api';

export default {
    namespace: 'supplement',

    state: {
        businessList: null,
        businessDetails: null,
        businessLoading: false,
        businessResult: null,
        businessPagination: null,
        businessMessage: null,
    },

    effects: {
        *fetchlImportList({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    businessLoading: true,
                },
            });
            const response = yield call(fetchlImportList, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        businessLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    businessList: response.data.list,
                    businessPagination: response.data.page,
                    businessLoading: false,
                }
            });
            if (callback) callback();
        },

        *fetchBusinessDetails({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    businessLoading: true,
                },
            });
            const response = yield call(fetchBusinessDetails, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        businessLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    businessDetails: response.data,
                    businessLoading: false,
                }
            });
            if (callback) callback();
        },
        
        *queryBusinessStatus({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    businessLoading: true,
                },
            });
            const response = yield call(queryBusinessStatus, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        businessLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    businessMessage: response.data,
                    businessResult: response.result,
                    businessLoading: false,
                }
            });
            if (callback) callback();
        },

        *submitBrack({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    businessLoading: true,
                },
            });
            const response = yield call(submitBrack, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        businessLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    businessMessage: response.data,
                    businessResult: response.result,
                    businessLoading: false,
                }
            });
            if (callback) callback();
        },

        *submitUploadVideo({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    businessLoading: true,
                },
            });
            const response = yield call(submitUploadVideo, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        businessLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    businessMessage: response.data,
                    businessResult: response.result,
                    businessLoading: false,
                }
            });
            if (callback) callback();
        },
        
        *againSubmitBrank({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    businessLoading: true,
                },
            });
            const response = yield call(againSubmitBrank, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        businessLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    businessMessage: response.data,
                    businessResult: response.result,
                    businessLoading: false,
                }
            });
            if (callback) callback();
        },
        
        *submitDetails({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    businessLoading: true,
                },
            });
            const response = yield call(submitDetails, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        businessLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    businessMessage: response.data,
                    businessResult: response.result,
                    businessLoading: false,
                }
            });
            if (callback) callback();
        },
    },

    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },
    },
};
